
package com.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bean.Client;
import com.bean.Marketing;
import com.bean.Receive;
import com.bean.User;
import com.service.ClientService;
import com.service.MarketingService;
import com.service.ReceiveService;
import com.service.UserService;


@Controller
@RequestMapping("receive")
public class ReciveAction {
	
	@Autowired
	private ReceiveService receiveService;

	@Autowired
	private ClientService clientService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private MarketingService ms;
	
	
	
	
	@RequestMapping("list")
	public String list(String name,Integer uid,String type,Model model){
		Map<String, Object> map = new HashMap<String,Object>();
		if(name != null && "".equals(name.trim())){
			name = null;
		}
		if(type != null && "".equals(type.trim())){
			type = null;
		}
		map.put("name", name);
		map.put("uid", uid);
		map.put("type", type);
		List<Receive> list = receiveService.list(map);
		
		System.out.println("大小="+list.size());
		model.addAttribute("list", list);
		return "receive/list";
	}
		
	
	@RequestMapping("toAdd")
	public String toAdd(String cid,Model model){
		Integer id = Integer.parseInt(cid);
		Client client = clientService.selectOne(id);
		model.addAttribute("client", client);
		return "receive/add";
	}
	
	@RequestMapping("add")
	public String add(Receive bean,Model model){
		System.out.println("q2eqwsdsadasdasdasdasd========");
		receiveService.insert(bean);
		return "redirect:list.do";
	}
	
	@RequestMapping("delete")
	public void delete(Integer id){
		receiveService.delete(id);
	}
	@RequestMapping("toUpdate")
	public String toUpdate(Integer id,Model model){
		
		Receive receive = receiveService.selectOne(id);
		Client client = clientService.selectOne(receive.getClientid());
		User user = userService.selectById(receive.getUserid());
		receive.setCname(client.getClientidname());
		receive.setUname(user.getUsername());
		
		model.addAttribute("receive", receive);
		return "receive/update";
	}
	
	
	
	@RequestMapping("update")
    public String update(Receive bean){
		receiveService.update(bean);
		return "redirect:list.do";
    }
	
	
	
	@RequestMapping("/jiedai.do")
    public String marketing(Integer ace,Model model){
		System.out.println("ace="+ace);
		
	   Marketing marketing = ms.selectByPrimaryKey(ace);
	   
	   Integer clientid = marketing.getClientid();
	   
	   Client client = clientService.selectOne(clientid);
	   
		Integer userid = marketing.getUserid();
	System.out.println("client.getClientidname()="+client.getClientidname());
	System.out.println("uid="+userid);
		
		model.addAttribute("cid",clientid);
		model.addAttribute("uid", userid);
		model.addAttribute("type", null);
		
		return "redirect:jiedailist.do";
    }
	
	
		
		
		@RequestMapping("/jiedailist.do")
    public String jiedailist(Integer cid,Integer uid,Model model){
		
			
			Map<String,Object> map=new HashMap<String, Object>();
			map.put("uid", uid);
			map.put("cid", cid);
			List<Receive> list = receiveService.selectByciduid(map);
			
			model.addAttribute("list", list);
			
		return "receive/jiedailist";
    }
	
	
	
	
	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
	}
}
